<template>
  <el-row class="login-container">
    <el-col :lg="16" :md="12" class="left">
      <div>
        <div>欢迎登录</div>
        <div>此站点是PM管理系统</div>
      </div>
    </el-col>
    <el-col :lg="8" :md="12" class="right">
      <h2 class="title">欢迎回来</h2>
      <div>
        <span class="line"></span>
        <span>账号密码登录</span>
        <span class="line"></span>
      </div>

      <el-form ref="formRef" :model="form" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="form.username" placeholder="请输入用户名">
            <template #prefix>
              <el-icon class="el-input__icon">
                <User />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input type="password" v-model="form.password" placeholder="请输入密码" show-password>
            <template #prefix>
              <el-icon class="el-input__icon">
                <Lock />
              </el-icon>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button class="w-[250px]" type="primary" round color="#626aef" @click="onSubmitForm"
            :loading="loading">登录</el-button>
        </el-form-item>
      </el-form>
    </el-col>
  </el-row>
</template>

<script setup name="Login">
import { ref, reactive, onMounted, onBeforeUnmount } from "vue";
import { Lock, User } from "@element-plus/icons-vue";
import { login } from "@/api/manager.js";
import { useRouter } from "vue-router";
import { setToken, getToken } from "@/composables/auth";
import { toast } from "@/composables/utils.js";
import { getinfo } from "@/api/manager.js";
import { useUserStore } from "@/stores/index.js";


const router = useRouter();
const formRef = ref();

const form = reactive({
  username: "",
  password: "",
});

const rules = {
  password: [
    { required: true, message: "密码不能为空", trigger: "blur" },
    { min: 2, max: 20, message: "密码长度在6-20位", trigger: "blur" },
  ],
  username: [
    { required: true, message: "用户名不能为空", trigger: "blur" },
    { min: 4, max: 12, message: "用户名长度在6-20位", trigger: "blur" },
  ],
};

const loading = ref(false);
const onSubmitForm = () => {
  formRef.value.validate((valid, fields) => {
    if (!valid) {
      return false;
    }
    loading.value = true;
    login(form.username, form.password)
      .then((res) => {
        console.log(res);
        toast("登录成功", "success")
        
        // 存储cookie
        setToken(res.token)

        const token = getToken()
        // 登录成功获取用户信息
        if (token) {
          const userStore = useUserStore();
          getinfo().then((res) => {
            userStore.setUserInfo(res);
          });
  }


        router.push("/");
      })
      .finally(() => {
        loading.value = false;
      });
  });
};

// 键盘监听事件
const onKeyUp = (e) => {
  if (e.key == "Enter") onSubmitForm()

}

onMounted(() => {
  document.addEventListener("keyup", onKeyUp)

})

onBeforeUnmount(() => {
  document.removeEventListener("keyup", onKeyUp)
})

</script>

<style scoped>
.login-container {
  @apply min-h-screen bg-indigo-500;
}

.login-container .left,
.login-container .right {
  @apply flex items-center justify-center;
}

.login-container .right {
  @apply bg-light-50 flex-col;
}

.left>div>div:first-child {
  @apply font-bold text-4xl text-light-50 mb-4;
}

.left>div>div> :last-child {
  @apply text-gray-200 text-sm;
}

.title {
  @apply font-bold text-3xl text-gray-800;
}

.right>div {
  @apply flex items-center justify-center my-5 text-gray-300 space-x-2;
}

.line {
  @apply h-[1px] w-16 bg-gray-200;
}
</style>
